import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.IOException;
import java.util.*;
import java.util.Timer;

public class PlayPanel extends JPanel {
    //属性
    static private int gamemode = 0;//0.准备界面  1.游戏界面  2.死亡界面  3.暂停界面
    static private ArrayList<ArmyPlane> armyPlanes = new ArrayList<>();
    static private ArrayList<Bullet> bullets = new ArrayList<>();
    static private ArrayList<Bullet> armybullets = new ArrayList<>();
    static private ArrayList<Boom> booms = new ArrayList<>();
    static private ArrayList<Bee> bees = new ArrayList<>();
    static private Boom heroboom = null;
    static private ArrayList<Integer> allscored = new ArrayList<>();
    static private int nowscore = 0;

    static private int time = 0;//游戏时间


    //构造器

    public PlayPanel() {

    }
    //方法
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(Game.background,0,0,this);
        g.drawImage(Game.background,479,0,this);

        switch (gamemode){
            case 0://开始菜单
                this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                g.drawImage(Game.startmenu,260,50,this);
                break;


            case 1://游戏中
                this.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(Game.transqarentcursor, new Point(0, 0), null));//将鼠标变为透明
                g.setFont(new Font("微软雅黑",Font.BOLD,20));
                if (HeroPlane.getHeroplane()!=null){
                    g.drawString("血量："+HeroPlane.getHeroplane().getBoold(),0,20);
                }else{
                    g.drawString("血量：0",0,20);
                }

                g.drawString("得分："+nowscore,0,50);

                if (HeroPlane.getHeroplane()!=null){
                    g.drawString("攻击力："+HeroPlane.getHeroplane().getAttack(),0,80);
                }else{
                    g.drawString("攻击力：0",0,80);
                }
                //爆炸物动画
                if (booms!=null){
                    for (int i = 0; i < booms.size(); i++) {
                        if (booms.get(i).getImage()==null){
                            booms.remove(i);
                        }else{
                            g.drawImage(booms.get(i).getImage(),booms.get(i).getX(),booms.get(i).getY(),booms.get(i).getWidth(),booms.get(i).getHight(),this);
                        }
                    }
                }

                //英雄机子弹动画
                if (bullets.size()>0){
                    for (Bullet b:bullets){
                        if (b.getImage()!=null&&b instanceof NormalBullet){
//                            g.drawImage(b.getImage(),b.getX(),b.getY(),this);
                            drawFlyer(g,b);
                        }
                    }
                }

                //敌机子弹动画
                if (armybullets.size()>0){
                    for (Bullet b:armybullets){
                        if (b.getImage()!=null&&b instanceof NormalBullet){
//                            g.drawImage(b.getImage(),b.getX(),b.getY(),this);
                            drawFlyer(g,b);
                        }else if (b instanceof LaserBullet){
                            g.setColor(Color.RED);
                            g.fillRect(b.getX(),b.getY(),b.getWidth(),b.getHight());
                            g.setColor(Color.BLACK);
                        }
                    }
                }

                //敌机动画
                if (armyPlanes.size()>0){
                    for (ArmyPlane army: armyPlanes) {
//                            g.drawImage(army.getImage(),army.getX(),army.getY(),army.getWidth(),army.getHight(),this);
                        drawFlyer(g,army);
                        //如果是BOSS敌机，显示血条
                        if (army instanceof ExtraPlane){
                            g.drawRect(army.getX(),army.getY()-5,army.getWidth(),5);
                            g.fillRect(army.getX(),army.getY()-5,army.getBoold()*army.getWidth()/200,5);
                        }
                    }
                }

                //小蜜蜂动画
                if (bees.size()>0){
                    for (Bee b:bees) {
                        drawFlyer(g,b);
                    }
                }


                //英雄机动画
//                g.drawImage(Heroplane.getHeroplane().getImage(), Heroplane.getHeroplane().getX(),Heroplane.getHeroplane().getY(),Heroplane.getHeroplane().getWidth(),Heroplane.getHeroplane().getHight(),this);
                if (HeroPlane.getHeroplane()!=null){
                    drawFlyer(g, HeroPlane.getHeroplane());
                }



                //英雄机阵亡，游戏结束
                if (heroboom!=null && heroboom.getImage()==null) {
                    gamemode = 2;

                }


                break;



            case 2://游戏结束
                this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                g.drawImage(Game.endmenu,0,0,StaticNumber.WINWID,StaticNumber.WINHIG,this);

                break;


            case 3://暂停
                this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                for (ArmyPlane army: armyPlanes) {
                    g.drawImage(army.getImage(),army.getX(),army.getY(),army.getWidth(),army.getHight(),this);
                }
                g.drawImage(HeroPlane.getHeroplane().getImage(), HeroPlane.getHeroplane().getX(), HeroPlane.getHeroplane().getY(),this);
                g.drawImage(Game.pusemenu,0,0,StaticNumber.WINWID,StaticNumber.WINHIG,this);
                break;
        }


    }



    static BufferedImage readImage(String path){
        BufferedImage img = null;
        try {
                    img = ImageIO.read(Game.class.getResource(path));
                } catch (IOException e) {
                    e.printStackTrace();
        }
        return img;
    }



    void action(){
        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (gamemode==1){
                    time++;
                }
                Rule.judge();
                repaint();
            }
        },0,1);
    }

    //画飞行物
    void drawFlyer(Graphics g, Flyer f){
        if (f.getWidth()==0&&f.getHight()==0){
            g.drawImage(f.getImage(),f.getX(),f.getY(),this);
        }else{
            g.drawImage(f.getImage(),f.getX(),f.getY(),f.getWidth(),f.getHight(),this);
        }
    }

    //游戏结束结算和清理
    static void clear(){
        System.out.println("开始清算");
        setGamemode(2);
        allscored.add(nowscore);
        nowscore = 0;
        armyPlanes = new ArrayList<>();
        bullets = new ArrayList<>();
        armybullets = new ArrayList<>();
        bees = new ArrayList<>();
        booms = new ArrayList<>();
        time = 0;
        heroboom = null;
        Rule.setArmycreattime(0);
        Rule.setProtecttime(0);
        Rule.setLeftoverBoss(8);

    }

    public static int getGamemode() {
        return gamemode;
    }

    public static void setGamemode(int gamemode) {
        PlayPanel.gamemode = gamemode;
    }

    public static int getTime() {
        return time;
    }

    public static void setTime(int time) {
        PlayPanel.time = time;
    }

    public static ArrayList<ArmyPlane> getArmyplanes() {
        return armyPlanes;
    }

    public static ArrayList<Bullet> getBullets() {
        return bullets;
    }

    public static ArrayList<Bullet> getArmybullets() {
        return armybullets;
    }

    public static ArrayList<Boom> getBooms() {
        return booms;
    }

    public static Boom getHeroboom() {
        return heroboom;
    }

    public static void setHeroboom(Boom heroboom) {
        PlayPanel.heroboom = heroboom;
    }

    public static ArrayList<Integer> getAllscored() {
        return allscored;
    }

    public static ArrayList<Bee> getBees() {
        return bees;
    }

    public static int getNowscore() {
        return nowscore;
    }

    public static void setNowscore(int nowscore) {
        PlayPanel.nowscore = nowscore;
    }
}
